//
//  ViewController.swift
//  stringModel
//
//  Created by Bailinhan on 2017/10/16.
//  Copyright © 2017年 Bailinhan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    var index:Int! = 0
    lazy var allArr: NSMutableArray = {
        () -> NSMutableArray in
        let tempallArr = NSMutableArray()
        //获取数据
        let strPath = Bundle.main.path(forResource: "Student", ofType: "plist")
        let arr : NSMutableArray = NSMutableArray(contentsOfFile: strPath!)!
        for dict in arr {
            let model : StudentModel = StudentModel.studentModelWithDict(dict as! NSDictionary)
            tempallArr.add(model)
        }
        return tempallArr
    }()
    lazy var titleLabel: UILabel = {
        () -> UILabel in
        let temtitleLabel = UILabel(frame:CGRect(x :60, y: 60, width: 200, height: 50))
        temtitleLabel.backgroundColor = UIColor.red
        self.view.addSubview(temtitleLabel)
        return temtitleLabel
    }()
    
    lazy var imgLabel: UIImageView = {
        () -> UIImageView in
        let tempimgView = UIImageView(frame: CGRect(x :60, y: 120, width: 200, height: 200))
        tempimgView.backgroundColor = UIColor.red
        self.view.addSubview(tempimgView)
        return tempimgView
    }()
    lazy var btnBack: UIButton = {
        () -> UIButton in
        let tempbtnBack = UIButton(frame: CGRect(x :60, y: 330, width: 95, height: 50))
        tempbtnBack.setTitle("上一张", for: UIControlState.normal)
        tempbtnBack.addTarget(self, action: #selector(setBack), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnBack)
        return tempbtnBack
    }()
    lazy var btnNext: UIButton = {
        () -> UIButton in
        let tempbtnNext = UIButton(frame: CGRect(x :165, y: 330, width: 95, height: 50))
        tempbtnNext.setTitle("下一张", for: UIControlState.normal)
        tempbtnNext.addTarget(self, action: #selector(setNext), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnNext)
        return tempbtnNext
    }()
    
    func setBack(){
        if self.index > 0 && self.index <= self.allArr.count - 1 {
            self.index = self.index - 1
            let model: StudentModel = self.allArr[self.index] as! StudentModel
            let titleStr = NSString(string: "\(model.name!) \(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgLabel.image = UIImage(named: model.pic as String)
            self.btnNext.isEnabled = true
            self.btnNext.backgroundColor = UIColor.blue
            if self.index == 0 {
                self.btnBack.isEnabled = false
                self.btnBack.backgroundColor = UIColor.gray
            }
            
        }
        
    }
    func setNext(){
        if self.index >= 0 && self.index < self.allArr.count - 1 {
            self.index = self.index + 1
            let model: StudentModel = self.allArr[self.index] as! StudentModel
            let titleStr = NSString(string: "\(model.name!) \(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgLabel.image = UIImage(named: model.pic as String)
            self.btnBack.isEnabled = true
            self.btnBack.backgroundColor = UIColor.blue
            if self.index == self.allArr.count - 1 {
                self.btnNext.isEnabled = false
                self.btnNext.backgroundColor = UIColor.gray
            }
            
        }
        
        
    }
    func showCurrentPage(iPage: Int){
        let model: StudentModel = self.allArr[iPage] as! StudentModel
        let titleStr = NSString(string:"\(model.name!) \(self.index+1)/\(self.allArr.count)")
        self.titleLabel.text = titleStr as String
        self.titleLabel.textAlignment = .center
        self.imgLabel.image = UIImage(named: model.pic as String)
        self.btnBack.backgroundColor = UIColor.blue
        self.btnNext.backgroundColor = UIColor.blue
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showCurrentPage(iPage: self.index)
        
    }
    
}
